package PagePackage;




import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import static org.testng.AssertJUnit.*;

import org.openqa.selenium.interactions.Actions;

import LoginPackage.TestBaseClass;


public class usersPageClass extends internalPageClass {
	
	@FindBy (className="btn-add-new")
	public WebElement addNewUserButton;
	
	@FindBy (xpath="html/body/main/section/aside/form/div[1]/input")
	public WebElement userEmail;
	@FindBy (className="btn-search")
	public WebElement searchButton;
	
	
    
	public void findEmail(String email) {
		 driver.findElement(By.xpath("//*[contains(.,email)]"));
	}
	
	public void emailDoesNotExist(String cancelEmail) {
		 assertFalse(driver.findElement(By.xpath("//*")).getText().equals(cancelEmail));
			}
	
	
	public userContactInfoPageClass scrollAndClick( int x, int y, String emailRemove) {
		
		WebElement removeroles=driver.findElement(By.xpath(emailRemove));
		 
	    String code = "window.scroll(" + (removeroles.getLocation().x + x) + ","
	                                 + (removeroles.getLocation().y + y) + ");";
	 
	    ((JavascriptExecutor)driver).executeScript(code, removeroles, x, y);
	  
		 
		 /*WebElement edit2 = removeroles.findElement(By.xpath("../td/button[contains(@class,'edit')]"));
		 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", edit2);*/
	    
	    
	    WebElement edit2 = removeroles.findElement(By.xpath("../../td[2]/div/a"));
		 ((JavascriptExecutor) driver).executeScript("arguments[0].click();", edit2);
		 return MyPageFactory.getPage(driver, userContactInfoPageClass .class);
	}
	
	
	
	public newUserPageClass scrollAndClick2(int x, int y, String userEmail) {
		 boolean present;
		    try {
		WebElement newUserClick=driver.findElement(By.xpath(userEmail));
		 
	  String code = "window.scroll(" + (newUserClick.getLocation().x + x) + ","
	                              + (newUserClick.getLocation().y + y) + ");";

	  ((JavascriptExecutor)driver).executeScript(code, newUserClick, x, y); 
	  
	      present = true;
	   } catch (NoSuchElementException e) {
	      present = false;
	   }
		    assertEquals(false, present);
		 return MyPageFactory.getPage(driver, newUserPageClass.class);
		 
	}
	public void clickTeamTab() {
		teamstab.click();
	}
	
	public void clickUserTab() {
		userstab.click();
	}
	
	public usersPageClass findUser(String email){
		userEmail.click();
		userEmail.clear();
		userEmail.sendKeys(email);
		searchButton.click();
		 return MyPageFactory.getPage(driver, usersPageClass.class);
	}
	public void openUser(String emailRemove)  {
		Actions actions = new Actions(driver);
		WebElement removeroles = driver.findElement(By.xpath(emailRemove));
		 
		actions.moveToElement(removeroles).perform();
		WebElement edit = removeroles.findElement(By.xpath("../td/button[contains(@class,'edit')]"));
		actions.moveToElement(edit).click().perform();
	}
	public boolean isOnThisPage(){
		return addNewUserButton.isDisplayed();
	}
	 @Override
		void tryToOpen() {
		 MyPageFactory.getPage(driver, loginPageClass.class).loginAs("victoria@krossover.com", "webmaster");
		}

	
	

	
}
